#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>

#include "tools.h"
#include "bank.h"

#define BUF_SIZE (4096)
//银行帐号后五位
int bank_id = 10001;
//服务端socket对象
int svr_fd;

//打印所有信息
void show(Account* acc)
{
	printf("%d %s %s %s %f %d\n",acc->type,acc->bank,acc->card,acc->pass,acc->balance,acc->islock);
}

//开户
void open_acc(Account* acc,char* buf)
{
	printf("%s\n",__func__);
	puts("用户正在进行开户...");
	sprintf(acc->bank,"123456%d",bank_id);
	sprintf(acc->pass,"123456");
	acc->islock = 0;
	char path[256];
	sprintf(path,"%s",acc->bank);
	int fd = open(path,O_WRONLY|O_CREAT|O_EXCL,0644);
	if(0>fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，开户失败");
		return;
	}
	
	sprintf(buf,"Y：开户成功，您的卡号为：%s 密码为：%s",acc->bank,acc->pass);
	bank_id++;
	write(fd,acc,sizeof(Account));
	close(fd);
}

//登录
void login(Account* acc, char* buf)
{
	printf("%s\n",__func__);
	puts("此用户正在进行开户登录");
	char path[256]={};
	sprintf(path,"%s",acc->bank);
	
	//判断是卡号否存在
	if(0 != access(path,F_OK))
	{
		sprintf(buf,"N:卡号不存在，请检查!");
		return;
	}
	int fd = open(path,O_RDWR);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，登陆失败!");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));
	
	//判断此帐号是否被锁定
	if(acc2.islock >= 3)
	{
		sprintf(buf,"N:此账号已经锁定，请解锁!");
		return;
	}
	
	if(strcmp(acc->pass,acc2.pass))
	{
		acc2.islock++;
		sprintf(buf,"N:密码错误，重新登陆（您还有%d次机会）!",3-acc2.islock);
		if(acc2.islock >= 3)
		{
			sprintf(buf,"N:此账号已经锁定，请解锁!");
		}
	}
	else
	{
		acc2.islock = 0;
		sprintf(buf,"Y:恭喜您登陆成功!");
	}
	lseek(fd,0,SEEK_SET);
	write(fd,&acc2,sizeof(Account));
	close(fd);
}
//销户
void destory(Account* acc,char* buf)
{
	char path[256] = {};
	sprintf(path,"%s",acc->bank);
	// 判断银行卡号是否正确
	if(0 != access(path,F_OK))
	{
		puts("此卡号不存在");
		sprintf(buf,"N:卡号不存在，请检查!");
		return;
	}
	
	int fd = open(path,O_RDWR);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，销户失败");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));
	close(fd);
	
	if(strcmp(acc2.card,acc->card))
	{
		sprintf(buf,"N:身份证号不正确，销户失败!");
		return;
	}
	
	if(strcmp(acc2.pass,acc->pass))
	{
		sprintf(buf,"N:密码不正确，销户失败!");
		return;
	}
	
	if(remove(path))
	{
		error("remove");
		sprintf(buf,"N:服务器正在升级，销户失败!");
		return;
	}
	
	sprintf(buf,"Y:销户成功，期待下次与你相遇!");
}
//解锁
void unlock(Account * acc,char * buf)
{
	char path[256] = {};
	sprintf(path,"%s",acc->bank);
	// 判断银行卡号是否正确
	if(0 != access(path,F_OK))
	{
		sprintf(buf,"N:卡号不存在，请检查!");
		return;
	}
	
	int fd = open(path,O_RDWR);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，解锁失败!");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));
	if(strcmp(acc2.card,acc->card))
	{
		sprintf(buf,"N:身份证号不正确，解锁失败!");
		return;
	}
	
	if(strcmp(acc2.pass,acc->pass))
	{
		sprintf(buf,"N:密码不正确，解锁失败!");
		return;
	}
	
	acc2.islock = 0;
	lseek(fd,0,SEEK_SET);
	write(fd,&acc2,sizeof(Account));
	close(fd);
	
	sprintf(buf,"N:解锁成功!");
}
//存钱
void save(Account* acc,char* buf)
{
	char path[256] = {};
	sprintf(path,"%s",acc->bank);
	
	int fd = open(path,O_RDWR);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，存款失败!");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));

	acc2.balance += acc->balance;
	lseek(fd,0,SEEK_SET);
	write(fd,&acc2,sizeof(Account));
	close(fd);
	
	sprintf(buf,"Y:存款成功，当前余额为:%g\n!",acc2.balance);
}

//取款
void take(Account* acc,char * buf)
{
	char path[256] = {};
	sprintf(path,"%s",acc->bank);
	
	int fd = open(path,O_RDWR);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，取款失败!");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));

	if(acc2.balance <  acc->balance)
	{
		sprintf(buf,"N:余额不足，取款失败。当前余额为:%g\n!",acc2.balance);
		return;
	}

	acc2.balance -= acc->balance;
	lseek(fd,0,SEEK_SET);
	write(fd,&acc2,sizeof(Account));
	close(fd);
	
	sprintf(buf,"N:取款成功，当前余额为:%g\n!",acc2.balance);
}
//查询
void query(Account* acc,char* buf)
{
	char path[256] = {};
	sprintf(path,"%s",acc->bank);
	
	int fd = open(path,O_RDONLY);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，查询失败!");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));
	close(fd);
	
	sprintf(buf,"N:查询成功，当前余额为:%g!\n",acc2.balance);
}

//转账
void transfer(Account* acc,char* buf)
{
	char src_path[256] = {} , dest_path[256];
	
	sprintf(src_path,"%s",acc->bank);
	sprintf(dest_path,"%s",acc->card);
	
	// 判断银行卡号是否正确
	if(0 != access(dest_path,F_OK))
	{
		sprintf(buf,"N:目标卡号不存在，请检查!");
		return;
	}
		
	int src_fd = open(src_path,O_RDWR);
	int dest_fd = open(dest_path,O_RDWR);
	if(0 > src_fd || 0 > dest_fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，转账失败");
		return;
	}
	
	Account src_acc = {} , dest_acc = {};
	read(src_fd,&src_acc,sizeof(Account));
	read(dest_fd,&dest_acc,sizeof(Account));

	if(src_acc.balance <  acc->balance)
	{
		sprintf(buf,"N:余额不足，转账失败。当前余额为:%g\n!",src_acc.balance);
		return;
	}
	if(strcmp(acc->bank,acc->card)==0)
	{
		sprintf(buf,"用户不能给自己转帐!");
		return;
	}

	src_acc.balance -= acc->balance;
	dest_acc.balance += acc->balance;
	
	
	lseek(src_fd,0,SEEK_SET);
	lseek(dest_fd,0,SEEK_SET);
	write(src_fd,&src_acc,sizeof(Account));
	write(dest_fd,&dest_acc,sizeof(Account));
	close(src_fd);
	close(dest_fd);
	
	sprintf(buf,"N:转账成功，当前余额为:%g\n!",src_acc.balance);
}


//修改密码
void repass(Account* acc,char* buf)
{
	char path[256] = {};
	sprintf(path,"%s",acc->bank);
	
	if(0 == strcmp(acc->pass,"-1"))
	{
		sprintf(buf,"N:两次输入的密码不相同,修改密码失败!");
		return;
	}
	
	int fd = open(path,O_RDWR,0644);
	if(0 > fd)
	{
		puts("卡号不存在！");
		return;
	}
	
	Account acc2 = {};
	read(fd,&acc2,sizeof(Account));
	strcpy(acc2.pass,acc->pass);
	printf("卡号：%s 修改密码成功 新密码为：%s 余额为：%g \n",acc2.bank,acc2.pass,acc2.balance);
	sprintf(buf,"卡号：%s 修改密码成功 新密码为：%s 余额为：%g ",acc2.bank,acc2.pass,acc2.balance);
	fd=open(path,O_RDWR|O_TRUNC,0644);
	write(fd,&acc2,sizeof(Account));
	close(fd);
	return;
	
}


void *server(void *arg)
{
	int cli_fd=*(int*) arg;
	Account* acc=malloc(sizeof(Account));
	for(;;)
	{
		char buf[BUF_SIZE];
		int recv_size=read(cli_fd,buf,BUF_SIZE);
		if(0 >= recv_size || 0 == strcmp(buf,"quit"))
		{
			printf("客户端%d退出\n",cli_fd);
			close(cli_fd);
			pthread_exit(NULL);
		}
		sscanf(buf,"%d %s %s %s %f %d",&acc->type,acc->bank,acc->card,acc->pass,&acc->balance,&acc->islock);
		switch(acc->type)
		{
			case M_OPEN:open_acc(acc,buf);break;			//开户
			case M_LOGIN:login(acc,buf);break;			//登录
			case M_DESTORY:destory(acc,buf);break;		//销户
			case M_UNLOCK:unlock(acc,buf);break;			//解锁
			case M_SAVE:save(acc,buf);break;				//存钱
			case M_TAKE:take(acc,buf);break;				//取款
			case M_QUERY:query(acc,buf);break;			//查询
			case M_TRANSFER:transfer(acc,buf);break;		//转账
			case M_REPASS:repass(acc,buf);	break;			//改密码
		}
		//发送数据
		write(cli_fd,buf,strlen(buf)+1);
	}
}

//服务器关闭
void sigint(int signum)
{
	close(svr_fd);
	printf("服务器关闭");
	exit(0);
}

int main()
{
	signal(SIGINT,sigint);
	printf("创建socket对象\n");
	//创建socket对象
	svr_fd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > svr_fd)
	{
		perror("socket");
		return -1;
	}
	
	//准备通信地址
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(10001);
	addr.sin_addr.s_addr = inet_addr("10.0.2.15");
	socklen_t addrlen = sizeof(addr);
	
	//绑定socket对象与地址
	if(bind(svr_fd,(struct sockaddr*)&addr,addrlen))
	{
		perror("bind");
		return -1;
	}
	
	//设置监听和排除数量
	if(listen(svr_fd,10))
	{
		perror("listen");
		return -1;
	}
	for(;;)
	{
		int cli_fd = accept(svr_fd,(struct sockaddr*)&addr,&addrlen);
		printf("现在是%d号用户正在操作",cli_fd);
		if(0 > cli_fd)
		{
			perror("accept");
			return -1;
		}
		pthread_t tid;
		pthread_create(&tid,NULL,server,&cli_fd);
	}
}

